﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using OCR.Function;
using OCR.SystemThread;
using OCR.Config;
using System.Globalization;
using MvCameraControl;

namespace OCR.SystemThread
{
    class CameraOnlineThread
    {
        public static bool CameraOnlineThread_Flowrec = false;
        public static Thread CameraOnlineFlow_Thread = null;
        public static int iCameraOnline_Step = 0;

        public static bool CameraOnlineThread_Pause = false;
        public static void CheckCameraOnline_Thread()
        {
            CameraOnlineThread_Flowrec = true;
            iCameraOnline_Step = 0;
            CameraOnlineFlow_Thread = new Thread(CameraOnlineThreadFlow);
            CameraOnlineFlow_Thread.IsBackground = true;
            CameraOnlineFlow_Thread.Start();
        }

        private static void CameraOnlineThreadFlow()
        {
            while (CameraOnlineThread_Flowrec)
            {
                if (!CameraOnlineThread_Pause)
                {
                    switch (iCameraOnline_Step)
                    {
                        case 5:

                            CameraReceiveThread.RefreshDeviceListNoOpen();
                            //Console.WriteLine("-----device.IsConnected: "+ CameraReceiveThread.device.IsConnected);
                            if (CameraReceiveThread.device == null)
                            {
                                Console.WriteLine("-----无相机设备-----");
                                OCR_Form.AppendLog("未连接到相机..." + DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss"), isError: true);
                                LocalInfo.CameraStateResult = "未连接到相机..." + DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss");
                                CameraRestartThread.iCameraRestart_Step = 5;
                                CameraOnlineThread_Pause = true;
                                iCameraOnline_Step = 0;
                            }
                            else
                            {
                                //Console.WriteLine("device:" + CameraReceiveThread.device.DeviceInfo.SerialNumber);
                                
                            }
                            Thread.Sleep(5000);
                            break;
                        case 10:
                            break;
                    }
                }


            }

        }
    }
}
